package cn.IT.action;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.IT.domain.Reply;
import cn.IT.domain.Topic;
import cn.IT.domain.User;
import cn.IT.service.IReplyService;
import cn.IT.util.JsonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

@Controller()
@Scope("prototype")


public class ReplyAction {
    
	private Topic topic;
	private User user;
	private Reply reply;
	private String searchContent;
	private String resultData; // 用于异步传输数据交互
	
	// 在插件包的情况下直接通过名称注入业务层的类
	@Autowired
	private IReplyService replyService;

    JsonConfig jsonConfig = new JsonConfig();  
    
    
	
	//根据帖子id获得所有回复
	public String getAllReplyByTopicId(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		jsonConfig.setExcludes(new String[]{"reply"});
		List<Reply> reply = replyService.findByTopicId(topic.getTopicId());
		if(reply != null){
			resultData = "{\"result\": \"success\"" + "," + "\"reply\":" + JSONArray.fromObject(reply,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//更新点赞数
	public String updateReplyLike(){
		Reply updatedreply = replyService.updateReplyLike(reply.getReplyId(), user.getUserId());
		if(updatedreply != null){
			resultData = "{\"result\": \"success\""  + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\""  + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//添加回复
	public String addReply(){
		Serializable replyId = replyService.addReply(reply);
		if(replyId != null){
			resultData = "{\"result\": \"success\""  + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\""  + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取用户自己的评论
	public String findAllReplyByUser(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		jsonConfig.setExcludes(new String[]{"reply"});
		List<Reply> reply = replyService.findAllReplyByUser(user);
		if( reply != null){
			resultData = "{\"result\": \"success\"" + "," + "\"reply\":" + JSONArray.fromObject(reply,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//获取提醒消息(帖子有新的评论)
	public String findNewsByTopicUser(){
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonUtil()); 
		jsonConfig.setExcludes(new String[]{"reply"});
		List<Reply> reply = replyService.findNewsByTopicUser(user);
		if( reply != null){
			resultData = "{\"result\": \"success\"" + "," + "\"reply\":" + JSONArray.fromObject(reply,jsonConfig) + "}";
			System.out.println(resultData);
			
		}else{
			resultData = "{\"result\": \"error\"" + "," + "\"msg\":" + "\"空空如也~\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//删除帖子
	public String delReply(){
		if (replyService.delReply(reply, user) == "success"){
			resultData = "{\"result\": \"success\"" +  "}" ;
			System.out.println(resultData);
		}else{
			resultData = "{\"result\": \"error\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}
	
	//更新消息的阅读状态
	public String updateNewsByTopicUser(){
		if (replyService.updateNewsByTopicUser(user) == "success"){
			resultData = "{\"result\": \"success\"" +  "}" ;
			System.out.println(resultData);
		}else{
			resultData = "{\"result\": \"error\"" + "}" ;
			System.out.println(resultData);
		}
		return "success";
	}

	public Topic getTopic() {
		return topic;
	}




	public void setTopic(Topic topic) {
		this.topic = topic;
	}




	public String getSearchContent() {
		return searchContent;
	}

	public void setSearchContent(String searchContent) {
		this.searchContent = searchContent;
	}

	public void setResultData(String resultData) {
		this.resultData = resultData;
	}
	
    @JSON(serialize=false)
	public String getResultData() {
		return resultData;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}



}
